<?php

/**
 *    Desc
 *
 *    @author    Garbin
 *    @usage    none
 */
class BuyerApp extends MemberbaseApp
{
	var $_buyer_mod;
    function __construct()
    {
        $this->BuyerApp();
    }
    function BuyerApp()
    {
        parent::__construct();
		$this->check_powers(USER_ADMIN);
		$this->_buyer_mod =& m('buyer');
    }
    function index()
    {
		$page = $this->_get_page();
		$buyers = $this->_buyer_mod->find(array(
			'fields' => '*, buyer.if_show',
			'join' => 'belongs_merchandiser, belongs_company',
			'count' => true,
			'limit' => $page['limit'],
			'order'=>'buyer.company_id,buyer.user_id'
		));
		
		$page['item_count'] = $this->_buyer_mod->getCount();
		$this->_format_page($page);
		$this->assign('page_info', $page);
		$this->assign('buyers', $buyers);
		
        $this->display('buyer.index.html');
        /* 当前位置 */
        $this->_curlocal(LANG::get('buyer_manage'));
    }
	
	function _assign_form()
	{
		$buyer = array(
			'if_show' => 1,
		);
		$this->assign('buyer', $buyer);
	}
	
	function add()
	{
		if (!IS_POST) {
			$this->_assign_form();
			$this->_get_company_list();
			$this->_get_user_list();
			
	        $this->display('buyer.form.html');
        	/* 当前位置 */
	        $this->_curlocal(LANG::get('buyer_manage'), 'index.php?app=buyer',
				Lang::get('add_new_buyer'));
		} else {
			$buyer_name = trim($_POST['buyer_name']);
			$company_id = intval($_POST['company_id']);
			$user_id = intval($_POST['user_id']);
			$if_show = intval($_POST['if_show']);
			
			if (empty($buyer_name)) {
				$this->show_warning('buyer_name_not_empty');
				return;
			}
//			if (!$this->_buyer_mod->unique($buyer_name)) {
//				$this->show_warning('buyer_is_exist');
//				return;
//			}
			if (!$company_id) {
				$this->show_warning('company_select_pls');
				return;
			}
			if (!$user_id) {
				$this->show_warning('user_select_pls');
				return;
			}
			
			$data = array(
				'buyer_name' => $buyer_name,
				'company_id' => $company_id,
				'user_id' => $user_id,
				'if_show' => $if_show
			);
			
			$buyer_id = $this->_buyer_mod->add($data);
			if (!$buyer_id) {
				$this->_error($this->_buyer_mod->get_error());
				return;
			}
			$this->show_message('add_buyer_success', Lang::get('buyer'), 'index.php?app=buyer');
		}
	}
		
	function edit()
	{
		$buyer_id = intval($_GET['buyer_id']);
		if (!$buyer_id) {
			$this->show_warning('buyer_not_exist');
			return;
		}
		if (!IS_POST) {
			$buyer = $this->_buyer_mod->get_info($buyer_id);
			if (!$buyer) {
				$this->show_warning('buyer_not_exist');
				return;
			}
			$this->assign('buyer', $buyer);
			$this->_get_company_list();
			$this->_get_user_list();
			
			$this->display('buyer.form.html');
        	/* 当前位置 */
	        $this->_curlocal(LANG::get('buyer_manage'), 'index.php?app=buyer',
				Lang::get('edit_buyer'));
		} else {
			$buyer_name = trim($_POST['buyer_name']);
			$company_id = intval($_POST['company_id']);
			$user_id = intval($_POST['user_id']);
			$if_show = intval($_POST['if_show']);
			
			if (empty($buyer_name)) {
				$this->show_warning('buyer_name_not_empty');
				return;
			}
//			if (!$this->_buyer_mod->unique($buyer_name, $buyer_id)) {
//				$this->show_warning('buyer_is_exist');
//				return;
//			}
			if (!$company_id) {
				$this->show_warning('company_select_pls');
				return;
			}
			if (!$user_id) {
				$this->show_warning('user_select_pls');
				return;
			}
			
			$data = array(
				'buyer_name' => $buyer_name,
				'company_id' => $company_id,
				'user_id' => $user_id,
				'if_show' => $if_show
			);
			$this->_buyer_mod->edit($buyer_id, $data);
			if ($this->_buyer_mod->get_error()) {
				$this->_error($this->_buyer_mod->get_error());
				return;
			}
			$this->show_message('edit_buyer_success', Lang::get('buyer'), 'index.php?app=buyer');
		}
	}
	
	function del()
	{
		$buyer_id = intval($_GET['buyer_id']);
		if (!$buyer_id) {
			$this->show_warning('buyer_not_exist');
			return;
		}
	
		$this->_buyer_mod->drop("buyer_id={$buyer_id}");
		if ($this->_buyer_mod->get_error()) {
			$this->_error($this->_buyer_mod->get_error());
			return;
		}
		$this->show_message('删除买手成功', Lang::get('buyer'), 'index.php?app=buyer');
		
	}
	
	function _get_company_list()
	{
		$company_mod =&m ('company');
		$companys = $company_mod->find(array(
			'conditions' => '',
		));
		$this->assign('companys', $companys);
	}
	
	function _get_user_list()
	{
		$user_mod =& m('member');
		$users = $user_mod->findAll(array(
			'conditions' => 'role = ' . USER_MERCHANDISER . ' AND if_show = 1',
			'fields' => 'user_id, user_name'
		));
		$this->assign('users', $users);
	}
	
	function check_buyer()
	{
		$buyer_name = empty($_GET['buyer_name']) ? null : trim($_GET['buyer_name']);
		$buyer_id = empty($_GET['id']) ? 0 : intval($_GET['id']);
		if (!$buyer_name) {
            echo ecm_json_encode(false);
            return;
		}
		echo ecm_json_encode($this->_buyer_mod->unique($buyer_name, $buyer_id));
	}
}

?>
